package com.sofa.server.controller;

import com.alipay.sofa.rpc.boot.runtime.param.BoltBindingParam;
import com.alipay.sofa.runtime.api.annotation.SofaClientFactory;
import com.alipay.sofa.runtime.api.client.ClientFactory;
import com.alipay.sofa.runtime.api.client.ServiceClient;
import com.alipay.sofa.runtime.api.client.param.BindingParam;
import com.alipay.sofa.runtime.api.client.param.ServiceParam;
import com.sofa.server.ClientAnnotatedBean;
import com.sofa.server.ClientFactoryBean;
import com.sofa.server.DirectServiceImpl;
import com.sofa.service.DirectService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liyang
 * @description TODO
 * @date 2021/6/23 15:00
 */

@RestController
public class TestController {

    @Resource
    private ClientFactoryBean clientFactoryBean;

    @Resource
    private ClientAnnotatedBean clientAnnotatedBean;

    @GetMapping("/test")
    public String test(){

        ClientFactory clientFactory = clientFactoryBean.getClientFactory();

        ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);

        ServiceParam serviceParam = new ServiceParam();
        serviceParam.setInterfaceType(DirectService.class);
        serviceParam.setInstance(new DirectServiceImpl());

        List<BindingParam> params = new ArrayList<BindingParam>();
        BindingParam serviceBindingParam = new BoltBindingParam();
        params.add(serviceBindingParam);
        serviceParam.setBindingParams(params);

        serviceClient.service(serviceParam);

        return "OK";

    }

    @GetMapping("/test2")
    public String test2(){
       // ClientFactory clientFactory = clientFactoryBean.getClientFactory();
       // ClientFactory clientFactory = clientAnnotatedBean.getClientFactory();


       // ServiceClient serviceClient = clientFactory.getClient(ServiceClient.class);
        ServiceClient serviceClient = clientAnnotatedBean.getServiceClient();

        ServiceParam serviceParam = new ServiceParam();
        serviceParam.setInterfaceType(DirectService.class);
        serviceParam.setInstance(new DirectServiceImpl());

        List<BindingParam> params = new ArrayList<BindingParam>();
        BindingParam serviceBindingParam = new BoltBindingParam();
        params.add(serviceBindingParam);
        serviceParam.setBindingParams(params);

        serviceClient.service(serviceParam);

        return "OK";

    }
}
